<?php
namespace Home\Controller;
use Common\Common\Classes\Message;
use Home\Model\CollegeService;
use Home\Model\StudentService;
use Home\Model\TeacherService;
use Think\Controller;
class AuthController extends Controller {
    //---------------登陆页面的跳转-----------------------------------------*****-------------------
    public function login() {
        if(!isset($_SESSION["name"]) || $_SESSION["name"]==""){      //没有session['name']这个信息表示没有登陆状态
            $this->display();           //所以跳转到登陆页面
        }
        else{
            $this->redirect("Index/index");      //有登陆信息的话就直接去主页面..
        }
    }
    //---------------登陆的检测(ajax检测)-------------------------------------*****------------------
    public function check_login(){
        if (IS_AJAX) {
//            if($this->checkVerify()) {
            if(I("post.isTeacher")=="true"){            //教师登陆
                $teacher = new TeacherService();
                $data=$teacher->check_login();
            }
            else{                                       //学生登陆
                $stu_sql = new StudentService();
                $data = $stu_sql->check_login();
            }
            if ($data->status==1) {
                if (I("post.remember")) {    // 记住登录状态
                    $remember = 604800;
                } else {
                    $remember = 86400;
                }
                cookie("id", I("post.number"), $remember); //cookie信息的存放
                cookie("pwd", I("post.password"), $remember);
                cookie("isTeacher", I("post.isTeacher"), $remember);
            }
//            } else{
//                $data["status"] = 3;
//                $data["message"] = "验证码输入错误";
//                $data = new Message(0,"验证码输入错误!","");
//            }
            $this->ajaxReturn($data);
        }
    }
    //---------------登出---------------------------------------------------------*****------------------
    public function logout() {
        cookie("number", null);
        cookie("password", null);
        session_destroy();  //清除session所有信息,保证安全退出系统!!!
        $this->success("退出登录成功",U('Home/Auth/login'),1);
    }
    //---------------注册跳转,向注册页面返回所有学校的基本信息-------------------*****-----------
    public function register(){
        $college = new CollegeService();
        $data = $college->getAllCollege();
        $this->assign("college", $data);
        $this->display();
    }
    //---------------教师的注册------------------*****-----------
    public function tea_register(){
        if(IS_AJAX){
            $teacher = new TeacherService();
            $data = $teacher->register();
            $this->ajaxReturn($data);
        }
    }
    //-----------------学生的注册-----------------*****--------------------------------------
    public function stu_register(){
        if(IS_AJAX) {           //需要的信息:stu_number  stu_password_hash  stu_name   stu_email   col_id
            $stu_sql = new StudentService();
            $data = $stu_sql->register();
            $this->ajaxReturn($data);
        }
    }
    //---------------注册时,检测一下注册的email是否已经存在--------------*****-------------------------------
    public function check_email(){                  //教师注册和学生注册通用的...post一个"email"..
        if(IS_AJAX){
            $teacher = new TeacherService();
            $data = $teacher->check_email();            //先检测教师
            if($data->status == 1){                 //教师表中没有这个邮箱就检测学生表
                $stu_sql = new StudentService();
                $data = $stu_sql->check_email();
            }
            $this->ajaxReturn($data);
        }
    }
    //---------------教师注册时,检测一下注册的教工号是否已经存在-------------------*****--------------------------------------
    public function check_tea_number(){
        if(IS_AJAX) {
            $teacher = new TeacherService();
            $data = $teacher->check_number();
            $this->ajaxReturn($data);
        }
    }
    //---------------学生注册时,检测一下注册的学号是否已经存在-------------------*****--------------------------------------
    public function check_stu_number(){
        if(IS_AJAX) {               //需要col_id  stu_number
            $stu_sql = new StudentService();
            $data = $stu_sql->check_number();
            $this->ajaxReturn($data);
        }
    }
    //---------------获得验证码(数字+英文)------------------------------------*****------------------------
    public function getVerify(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 20;         //字体大小
        $Verify->length   = 4;          //长度
        $Verify->useNoise = false;      //不加杂点
        $Verify->imageW = 150;          //宽
        $Verify->imageH = 50;           //高
        $Verify->entry();
    }
    //---------------验证输入的验证码是否正确-------------------------------------*****-----------------------
    public function checkVerify(){
        $verify = new \Think\Verify();      //获得对象
        $verify_code = I("post.verify");
        return $verify->check($verify_code,"");
    }
}